function [] = plotTimeSeries(M, outputFile, varargin)
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	% Example use:
	%	plotTimeSeries(M, outputFile);
	%	plotTimeSeries(M, outputFile, 'Title : Y over time', 'yTitle: Y', {'Var1', 'Var2', 'Holiday indicator'}, {false, false, true}, {'black', [0.5 0.5 0.5], 'black'}, {1,1,1});
	%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	if length(varargin) >= 1
		mytitle = varargin{1};
	else
		mytitle = '';
	end
	if length(varargin) >= 2
		ytitle = varargin{2};
	else
		ytitle = '';
	end
	if length(varargin) >= 3
		varNames4legend = varargin{3};
	else
		varNames4legend = {};
	end
	if length(varargin) >= 4
		varIsVerticalIndicator = varargin{4};
	else
		varIsVerticalIndicator = {};
	end
	if length(varargin) >= 5
		lineColors = varargin{5};
	else
		lineColors = {};
	end
	if length(varargin) >= 6
		lineWidths = varargin{6};
	else
		lineWidths = {};
	end
	if length(varargin) >= 7
		legendLocation = varargin{7};
	else
		legendLocation = 'northwest';
	end
	if length(varargin) >= 8
		ylimits = varargin{8};
	else
		ylimits = {};
	end
	if length(varargin) >= 9
		xtitle = varargin{9};
	else
		xtitle = 'Time';
	end
	if length(varargin) >= 10
		lineStyles = varargin{10};
	else
		lineStyles = {};
	end
	if length(varargin) >= 11
		titleFontSize = varargin{11};
	else
		titleFontSize = 18;
	end
	if length(varargin) >= 12
		axisTitleFontSize = varargin{12};
	else
		axisTitleFontSize = 14;
	end
	if length(varargin) >= 13
		xtickLabelFontSize = varargin{13};
	else
		xtickLabelFontSize = 15;
	end

    % Read and check dimensions
    [NumPeriods, NumCols] = size(M);
	if NumPeriods == 0 || NumCols <= 1
		error('No data');
	end
	NumVars = NumCols - 1;

	% Put default values for empty fields
	if isempty(varNames4legend)
		varNames4legend = sprintfc('Var%d',1:NumVars);
	end
	if isempty(varIsVerticalIndicator)
		for vv = 1:NumVars; varIsVerticalIndicator{vv} = false; end;
	end
	if isempty(lineWidths)
		for vv = 1:NumVars; lineWidths{vv} = 1; end;
	end
	if isempty(lineStyles)
		for vv = 1:NumVars; lineStyles{vv} = '-'; end;
	end
	
	% Check consistency of dimensions
	if length(varNames4legend) ~= NumVars || length(varIsVerticalIndicator) ~= NumVars || length(lineWidths) ~= NumVars || (~isempty(lineColors) && length(lineColors) ~= NumVars)
		error('Inconsistent dimensions of variable names with data.');
	end
	
    % Get labels and values
    periodLabels      = table2cell(M(:,1));    
    values            = table2array(M(:,2:end));
    NumPeriods        = length(values);

	% Get values to be displayed on x axis
	[xticks_values, xticks_values_disp] = makeXticks4timePlot(periodLabels);
    
    % Make figure + plot time series and vertical indicators
    h = figure;
	set(gcf, 'Units', 'Inches', 'Position', [0, 0, 10, 7], 'PaperUnits', 'Inches', 'PaperSize', [10, 7]);
    for vv = 1:NumVars
    	myvalues = values(:,vv);
    	if ~varIsVerticalIndicator{vv}	% usual plot over time
    		if ~isempty(lineColors)
	    		plot(myvalues,'LineWidth', lineWidths{vv}, 'LineStyle', lineStyles{vv}, 'Color', lineColors{vv});
	    	else
	    		plot(myvalues,'LineWidth', lineWidths{vv}, 'LineStyle', lineStyles{vv});
	    	end
			if ~isempty(ylimits)
				ylim(ylimits);
			end
    	else							% set of vertical lines
			myIdxes = find(myvalues>0);
			if isempty(ylimits)
				ylimits = ylim;
			end
    		ymin = ylimits(1);
    		ymax = ylimits(2);
    		for tt = myIdxes
    			hold on;
    			if ~isempty(lineColors)
    				plot([tt tt],[ymin ymax], 'LineWidth', lineWidths{vv}, 'Color', lineColors{vv}, 'LineStyle', lineStyles{vv});
	    		else
	    			plot([tt tt],[ymin ymax], 'LineWidth', lineWidths{vv}, 'Color', 'black', 'LineStyle', lineStyles{vv});
	    		end
			end
    	end
    	if vv < NumVars
    		hold on;
    	else
    		hold off;
    	end
    end
%    xlim([0,NumPeriods]); % puts a space on the left
    xlim([1,NumPeriods]); % solves the issue: it removes the space on the left

	% Make titles and legend	
	title(mytitle, 'Fontsize', titleFontSize);
	if ~isempty(xtitle)
		xlabel(xtitle, 'FontSize', axisTitleFontSize);
	end
	ylabel(ytitle, 'FontSize', axisTitleFontSize);
	if ~strcmp(legendLocation, 'noLegend')
		[legh,objh] = legend(varNames4legend, 'Location', legendLocation, 'FontSize',14);
		
		%%% Case where we want vertical lines to appear in the legend
		verticalLine2AppearInLgd = false;
		for vv = 1:NumVars
	    	if varIsVerticalIndicator{vv} && ~isempty(varNames4legend{vv})
	    		verticalLine2AppearInLgd = true;
			end
		end
		if verticalLine2AppearInLgd
			lineh = findobj(objh,'type','line');
			for vv = 1:NumVars
	    		if varIsVerticalIndicator{vv}	% usual plot over time
					set(lineh(2*vv-1), 'LineWidth', lineWidths{vv}, 'LineStyle', lineStyles{vv});
					set(lineh(2*vv), 'LineWidth', lineWidths{vv}, 'LineStyle', lineStyles{vv});
				end
			end
		end
	end
	
	% Print xticks
	xticks(xticks_values); xticklabels(xticks_values_disp); xtickangle(45);
	a = get(gca,'XTickLabel');
	set(gca,'XTickLabel',a,'fontsize',xtickLabelFontSize)
    
    % Reformat yticks to avoid scientific notation
	set(gca,'YTickLabel',formatNumbersWith1000commas(get(gca,'YTick'))');
	
	% Save to output file
	print(h, '-dpdf', outputFile);
	close(h);
end
